package com.prawn.ift_collequipment.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift_collequipment.dao.EquipmentAssociateDao;
import com.prawn.ift_collequipment.pojo.EquipmentAssociate;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class EquipmentAssociateService {

	@Autowired
	private EquipmentAssociateDao equipmentAssociateDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<EquipmentAssociate> findAll() {
		return equipmentAssociateDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<EquipmentAssociate> findSearch(Map whereMap, int page, int size) {
		Specification<EquipmentAssociate> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return equipmentAssociateDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<EquipmentAssociate> findSearch(Map whereMap) {
		Specification<EquipmentAssociate> specification = createSpecification(whereMap);
		return equipmentAssociateDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "equipmentAssociate",key = "#id")
	public EquipmentAssociate findById(String id) {
		return equipmentAssociateDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param equipmentAssociate
	 */
	public void add(EquipmentAssociate equipmentAssociate) {
		equipmentAssociate.setId( idWorker.nextId()+"" );
		equipmentAssociateDao.save(equipmentAssociate);
	}

	/**
	 * 修改
	 * @param equipmentAssociate
	 */
	@CacheEvict(value = "equipmentAssociate",key = "#equipmentAssociate.id")
	public void update(EquipmentAssociate equipmentAssociate) {
		equipmentAssociateDao.save(equipmentAssociate);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "equipmentAssociate",key = "#id")
	public void deleteById(String id) {
		equipmentAssociateDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<EquipmentAssociate> createSpecification(Map searchMap) {

		return new Specification<EquipmentAssociate>() {

			@Override
			public Predicate toPredicate(Root<EquipmentAssociate> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 设备编号
                if (searchMap.get("number")!=null && !"".equals(searchMap.get("number"))) {
                	predicateList.add(cb.like(root.get("number").as(String.class), "%"+(String)searchMap.get("number")+"%"));
                }
                // 设备名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 类型
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                	predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 设备连接地址
                if (searchMap.get("deviceUrl")!=null && !"".equals(searchMap.get("deviceUrl"))) {
                	predicateList.add(cb.like(root.get("deviceUrl").as(String.class), "%"+(String)searchMap.get("deviceUrl")+"%"));
                }
                // 设备厂家
                if (searchMap.get("manufactor")!=null && !"".equals(searchMap.get("manufactor"))) {
                	predicateList.add(cb.like(root.get("manufactor").as(String.class), "%"+(String)searchMap.get("manufactor")+"%"));
                }
                // 设备ip
                if (searchMap.get("ipAddr")!=null && !"".equals(searchMap.get("ipAddr"))) {
                	predicateList.add(cb.like(root.get("ipAddr").as(String.class), "%"+(String)searchMap.get("ipAddr")+"%"));
                }
                // 网络类型
                if (searchMap.get("networkType")!=null && !"".equals(searchMap.get("networkType"))) {
                	predicateList.add(cb.like(root.get("networkType").as(String.class), "%"+(String)searchMap.get("networkType")+"%"));
                }
                // 关联的采集设备id
                if (searchMap.get("equipmentId")!=null && !"".equals(searchMap.get("equipmentId"))) {
                	predicateList.add(cb.like(root.get("equipmentId").as(String.class), "%"+(String)searchMap.get("equipmentId")+"%"));
                }
                // 用户
                if (searchMap.get("auth")!=null && !"".equals(searchMap.get("auth"))) {
                	predicateList.add(cb.like(root.get("auth").as(String.class), "%"+(String)searchMap.get("auth")+"%"));
                }
                // 秘钥
                if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
                	predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
                }
                // 设备状态
                if (searchMap.get("deviceStatus")!=null && !"".equals(searchMap.get("deviceStatus"))) {
                	predicateList.add(cb.like(root.get("deviceStatus").as(String.class), "%"+(String)searchMap.get("deviceStatus")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
